package com.stu.thread;

import java.util.ArrayList;
import java.util.List;

public class Test {
  public static void main(String[] args) throws Exception {
    List<String> list = new ArrayList<>();
    for (int i = 0; i < 10000; i++) {
      new Thread(() -> {
        // 不同步的情况
        // list.add(Thread.currentThread().getName());
        // 同步的情况
        synchronized(list) {
          list.add(Thread.currentThread().getName());
        }
      }).start();
    }
    Thread.sleep(1000);
    System.out.println(list.size());
  }
}


class Account {
  private int count;
  // public void add() {
  //   for (int i = 0; i < 100000; i++) {
  //     count++;
  //   }
  // }
  public synchronized void add() {
    for (int i = 0; i < 100000; i++) {
      count++;
    }
  }
  public int getCount() {
    return count;
  }
}


class Test1 {
  public static void main(String[] args) throws InterruptedException {
    Account account = new Account();
    new Thread(() -> {
      account.add();
      System.out.println(Thread.currentThread().getName() + account.getCount());
    }, "线程一：").start();
    new Thread(() -> {
      account.add();
      System.out.println(Thread.currentThread().getName() + account.getCount());
    }, "线程二：").start();
  }
}